#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h> /* Inclusion du header de SDL_image (adapter le dossier au besoin) */
# include "SDL/SDL_mixer.h"

Uint8 r=10,g=10,b=10,r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4;
void sawer (SDL_Surface* img,int x ,int y ,SDL_Surface* ecr);
void getPixelColor(int x, int y, SDL_Surface *map,Uint8 *r,Uint8 *g,Uint8 *b);
void paint();
void paintflous();
void paintstop();
void paintstopflous();
void coli ();
int verif=0 ;
int x;
int nagz;
int i=0 ;
int j=0 ;
int l=0;
int k=0 ;
int x1=0 ;
int c=1;
int action1=0,test,testf=0;
int action2=0;
int continuer = 1, jump=0;
int indice = 0;
int indicetayara=0,indicepara=0;
int indicebombe = 0;
int indicespace = 9;
int espace =0;
int keyR = 0, keyUP = 0;
int ppx=0,ppy=460,avionx=650,aviony=0,flousx=250,flousy=160,bombx=200,bomby=460;
int parax=365,paray=460,gerrierx=400,gerriery=460,sqx=1200,sqy=460,sqx1=1300,sqy1=460,sqx2=1400,sqy2=460,sqx3=1500,sqy3=460,explx=500,exply=460,fondx=0,fondy=0;
SDL_Event event;
SDL_Surface *ecran = NULL,
*tayara[5],*gerr[5],*parachute[5],*back1=NULL,*bombe[28], *perso[10],*flous[5],*squelette[29],*explo[14]; 


Mix_Music *music;
     Mix_Music *music1;
int main()
{



SDL_Init(SDL_INIT_VIDEO);

Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 1024 );
   // SDL_WM_SetIcon(IMG_Load("sdl_icone.bmp"), NULL);

    ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE |SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Mohamed Mehmli LVL", NULL);
 SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
 



     gerr[0]= IMG_Load("monjeu/guerrier1.gif");
     gerr[1]= IMG_Load("monjeu/guerrier2.gif");
     gerr[2]= IMG_Load("monjeu/guerrier3.gif");
     gerr[3]= IMG_Load("monjeu/guerrier4.gif");
     gerr[4]= IMG_Load("monjeu/guerrier5.gif");
     gerr[5]= IMG_Load("monjeu/guerrier6.gif");

   back1= IMG_Load("monjeu/monback.png");

     parachute[0]= IMG_Load("monjeu/frame-1.png");
     parachute[1]= IMG_Load("monjeu/frame-2.png");
     parachute[2]= IMG_Load("monjeu/frame-3.png");
     parachute[3]= IMG_Load("monjeu/frame-4.png");
     parachute[4]= IMG_Load("monjeu/frame-5.png");
     parachute[5]= IMG_Load("monjeu/frame-6.png");

     bombe[0]= IMG_Load("monjeu/nar1.png");
     bombe[1]= IMG_Load("monjeu/nar2.png");
     bombe[2]= IMG_Load("monjeu/nar3.png");
     bombe[3]= IMG_Load("monjeu/nar4.png");
     bombe[4]= IMG_Load("monjeu/nar5.png");
     bombe[5]= IMG_Load("monjeu/nar6.png");
     bombe[6]= IMG_Load("monjeu/nar7.png");
     bombe[7]= IMG_Load("monjeu/nar8.png");
     bombe[8]= IMG_Load("monjeu/nar9.png");
     bombe[9]= IMG_Load("monjeu/nar10.png");
     bombe[10]= IMG_Load("monjeu/nar11.png");
     bombe[11]= IMG_Load("monjeu/nar12.png");
     bombe[12]= IMG_Load("monjeu/nar13.png");
     bombe[13]= IMG_Load("monjeu/nar14.png");
     bombe[14]= IMG_Load("monjeu/nar15.png");
     bombe[15]= IMG_Load("monjeu/nar16.png");
     bombe[16]= IMG_Load("monjeu/nar17.png");
     bombe[17]= IMG_Load("monjeu/nar18.png");
     bombe[18]= IMG_Load("monjeu/nar19.png");
     bombe[19]= IMG_Load("monjeu/nar20.png");
     bombe[20]= IMG_Load("monjeu/nar21.png");
     bombe[21]= IMG_Load("monjeu/nar22.png");
     bombe[22]= IMG_Load("monjeu/nar23.png");
     bombe[23]= IMG_Load("monjeu/nar24.png");
     bombe[24]= IMG_Load("monjeu/nar25.png");
     bombe[25]= IMG_Load("monjeu/nar26.png");
     bombe[26]= IMG_Load("monjeu/nar.27png");
     bombe[27]= IMG_Load("monjeu/nar.28png");
     
     tayara[0]= IMG_Load("monjeu/tayara1.png");
   tayara[1]= IMG_Load("monjeu/tayara2.png");
     tayara[2]= IMG_Load("monjeu/tayara3.png");
     tayara[3]= IMG_Load("monjeu/tayara4.png");
     tayara[4]= IMG_Load("monjeu/tayara5.png");


   squelette[0]= IMG_Load("monjeu/frame-01.png");
   squelette[1]= IMG_Load("monjeu/frame-02.png");
   squelette[2]= IMG_Load("monjeu/frame-03.png");
   squelette[3]= IMG_Load("monjeu/frame-04.png");
   squelette[4]= IMG_Load("monjeu/frame-05.png");
   squelette[5]= IMG_Load("monjeu/frame-06.png");
   squelette[6]= IMG_Load("monjeu/frame-07.png");
   squelette[7]= IMG_Load("monjeu/frame-08.png");
   squelette[8]= IMG_Load("monjeu/frame-09.png");
   squelette[9]= IMG_Load("monjeu/frame-10.png");
   squelette[10]= IMG_Load("monjeu/frame-11.png");
   squelette[11]= IMG_Load("monjeu/frame-12.png");
   squelette[12]= IMG_Load("monjeu/frame-13.png");
   squelette[13]= IMG_Load("monjeu/frame-14.png");
   squelette[14]= IMG_Load("monjeu/frame-15.png");
   squelette[15]= IMG_Load("monjeu/frame-16.png");
   squelette[16]= IMG_Load("monjeu/frame-17.png");
   squelette[17]= IMG_Load("monjeu/frame-18.png");
   squelette[18]= IMG_Load("monjeu/frame-19.png");
   squelette[19]= IMG_Load("monjeu/frame-20.png");
   squelette[20]= IMG_Load("monjeu/frame-21.png");
   squelette[21]= IMG_Load("monjeu/frame-22.png");
   squelette[22]= IMG_Load("monjeu/frame-23.png");
   squelette[23]= IMG_Load("monjeu/frame-24.png");
   squelette[24]= IMG_Load("monjeu/frame-25.png");
   squelette[25]= IMG_Load("monjeu/frame-26.png");
   squelette[26]= IMG_Load("monjeu/frame-27.png"); 
   squelette[27]= IMG_Load("monjeu/frame-28.png");
   squelette[28]= IMG_Load("monjeu/frame-29.png");
   squelette[29]= IMG_Load("monjeu/frame-30.png");



explo[0]=IMG_Load("monjeu/expl1.png");
explo[1]=IMG_Load("monjeu/expl2.png");
explo[2]=IMG_Load("monjeu/expl3.png");
explo[3]=IMG_Load("monjeu/expl4.png");
explo[4]=IMG_Load("monjeu/expl5.png");
explo[5]=IMG_Load("monjeu/expl6.png");
explo[6]=IMG_Load("monjeu/expl7.png");
explo[7]=IMG_Load("monjeu/expl8.png");
explo[8]=IMG_Load("monjeu/expl9.png");
explo[9]=IMG_Load("monjeu/expl10.png");
explo[10]=IMG_Load("monjeu/expl11.png");
explo[11]=IMG_Load("monjeu/expl12.png");
explo[12]=IMG_Load("monjeu/expl13.png");
explo[13]=IMG_Load("monjeu/expl14.png");
explo[14]=IMG_Load("monjeu/expl15.png");



/**/
flous[0] = IMG_Load("flous/1.png");
flous[1] = IMG_Load("flous/2.png");
flous[2] = IMG_Load("flous/3.png");
flous[3] = IMG_Load("flous/4.png");
flous[4] = IMG_Load("flous/5.png");
flous[5] = IMG_Load("flous/6.png");

/**/




  perso[0] = IMG_Load("monjeu/1.png");
 perso[1] = IMG_Load("monjeu/2.png");
 perso[2] = IMG_Load("monjeu/3.png");
 perso[3] = IMG_Load("monjeu/4.png");
 perso[4] = IMG_Load("monjeu/5.png");
 perso[5] = IMG_Load("monjeu/6.png");
 perso[6] = IMG_Load("monjeu/7.png");
 perso[7] = IMG_Load("monjeu/8.png");
 perso[8] = IMG_Load("monjeu/9.png");
 perso[9] = IMG_Load("monjeu/10.png");
 perso[10] = IMG_Load("monjeu/11.png");
 

/**/
if(testf==0)
{
paintflous();
}
else
{
paint();
}
/**/
music = Mix_LoadMUS("monjeu/nagz.mp3");
music1 = Mix_LoadMUS("monjeu/helicopter.mp3");



SDL_EnableKeyRepeat(100, 100);
 while (continuer)
    {

coli ();
        SDL_PollEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
break;
 case SDL_KEYDOWN:
switch (event.key.keysym.sym)
            {
                case SDLK_RIGHT: /* Appui sur la touche entrer la variable entrer recoit 1 */
keyR=1;


		break;
                case SDLK_SPACE:
espace=1;
break ;
                  case SDLK_UP:
keyUP=1;


                 
        }
break;

case SDL_KEYUP:
switch (event.key.keysym.sym)
            {
                case SDLK_RIGHT: /* Appui sur la touche entrer la variable entrer recoit 1 */
keyR=0;
test=0;


		break;
                case SDLK_SPACE:
espace=0;
break ;
                  case SDLK_UP:
keyUP=0;

      break;
        }
break;

 }

if((ppx==flousx-50) && (ppy<=flousy+83)) 
{
testf=1;
}
if (espace ==1)
{
if (indicespace<10)
{
indicespace++;
}
else
{
indicespace=0;
  }
coli();
ppy-=40;
fondx-=5;
flousx-=5;

sqx-=15;
sqx1-=15;
sqx2-=15;
sqx3-=15;
if (fondx<-500)
{

paray+=15;
}
paintstopflous();
paintstop();
}


if (fondx<-500 )
{

Mix_PlayMusic(music1,1);
}






if(keyR==1 )
{

if (indicebombe<26)
{
indicebombe++;
}
else
{
indicebombe=0;
  }
if (indicepara<4)
{
indicepara++;
}
else
{
indicepara=0;
  }
coli();

test=1;

if (indicetayara<3)
{
indicetayara++;
}
else
{
indicetayara=0;
  }
if (indice<8)
{
indice++;
}
else
{
indice=0;
  }
if (l<13)
{
l++;
}
else
{
l=0;
  }
if (k<28)
{
k++;
}
else
{
k=0;
  }

sawer(squelette[k], sqx, sqy, ecran);
sawer(squelette[k], sqx1, sqy1, ecran);
sawer(squelette[k], sqx2, sqy1, ecran);
sawer(squelette[k], sqx3, sqy1, ecran);
sawer(explo[l], explx, exply, ecran);
sawer(bombe[indicebombe], bombx, bomby, ecran);
sawer(tayara[indicetayara], avionx, aviony, ecran);
if (fondx<-500)
{
sawer(gerr[indicepara],gerrierx, gerriery, ecran);
paray+=15;
}
fondx-=15;
flousx-=15;
sqx-=15;
sqx1-=15;
sqx2-=15;
sqx3-=15;


if(testf==0)
{
paintflous();
}
else
{
paint();
}

}




if(keyR==0 )
{
if (indicepara<4)
{
indicepara++;
}
else
{
indicepara=0;
  }
if (indicebombe<26)
{
indicebombe++;
}
else
{
indicebombe=0;
  }
if (indicetayara<3)
{
indicetayara++;
}
else
{
indicetayara=0;
  }
if (l<13)
{
l++;
}
else
{
l=0;
  }
if (k<28)
{
k++;
}
else
{
k=0;
  }
coli();
sawer(squelette[k], sqx, sqy, ecran);
sawer(squelette[k], sqx1, sqy1, ecran);
sawer(squelette[k], sqx2, sqy1, ecran);
sawer(squelette[k], sqx3, sqy1, ecran);
sawer(explo[l], explx, exply, ecran);
sawer(bombe[indicebombe], bombx, bomby, ecran);
sawer(tayara[indicetayara], avionx, aviony, ecran);

sqx-=15;
sqx1-=15;
sqx2-=15;
sqx3-=15;

if (fondx<-500)
{
sawer(gerr[indicepara],gerrierx, gerriery, ecran);
}

if(testf==0)
{
paintstopflous();
}
else
{
paintstop();
}

}





if(keyUP==1 )
{
Mix_PlayMusic(music,1);
if (indicepara<4)
{
indicepara++;
}
else
{
indicepara=0;
  }
coli();
if (indicetayara<3)
{
indicetayara++;
}
else
{
indicetayara=0;
  }

if (indicebombe<26)
{
indicebombe++;
}
else
{
indicebombe=0;
  }
if (indice<8)
{
indice++;
}
else
{
indice=0;
  }
ppy-=80;
fondx-=10;
flousx-=10;

sqx-=15;
sqx1-=15;
sqx2-=15;
sqx3-=15;


if(testf==0)
{
paintstopflous();
}
else
{
paintstop();
}

}


}


SDL_FreeSurface(perso[11]);


//Mix_FreeChunk(son);
//Mix_CloseAudio();

 return EXIT_SUCCESS;
}












void sawer (SDL_Surface* img,int x ,int y ,SDL_Surface* ecr)
{
SDL_Rect pos;
pos.x=x;
pos.y=y;
SDL_BlitSurface(img,NULL,ecr,&pos);
}
void getPixelColor(int x, int y, SDL_Surface *map,Uint8 *r,Uint8 *g,Uint8 *b) /*Collision .. */
{
    if(x>0 && y>0 && x<map->w && y<map->h)
    {
        int bpp = map->format->BytesPerPixel;
      
        Uint8 *p = (Uint8 *)map->pixels + y * map->pitch + x * bpp;
        SDL_GetRGB(*(Uint32 *)p,map->format, r, g, b);
    }
    else // sinon on renvoie noir, pour éviter de planter dans certains cas
    {
        *r =0;
        *g =0;
        *b =0;
    } 
}



void paint()
{

//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));


sawer(back1, fondx, fondy, ecran);

sawer(squelette[k], sqx, sqy, ecran);
sawer(squelette[k], sqx1, sqy1, ecran);
sawer(squelette[k], sqx2, sqy1, ecran);
sawer(squelette[k], sqx3, sqy1, ecran);

if (espace==0)
{
sawer(perso[indice], ppx, ppy, ecran);
} 
if (espace==1)
{
sawer(perso[indicespace], ppx, ppy, ecran);
}
sawer(explo[l], explx, exply, ecran);
sawer(bombe[indicebombe], bombx, bomby, ecran);

if (fondx<-500 )
{
avionx-=8;
sawer(tayara, avionx, aviony, ecran);

}
if (fondx<-600 )
{
sawer(parachute[indicepara] ,avionx, aviony, ecran);
paray+=15;

}
if (fondx<-500)
{
sawer(gerr[indicepara],gerrierx, gerriery, ecran);
}
SDL_Flip(ecran);
//end affichage

}

void paintflous()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));

sawer(back1, fondx, fondy, ecran);


if(i<5)
{
i++;
}
else
{
i=0;
}
sawer(squelette[k], sqx, sqy, ecran);
sawer(squelette[k], sqx1, sqy1, ecran);
sawer(squelette[k], sqx2, sqy2, ecran);
sawer(squelette[k], sqx3, sqy3, ecran);
sawer(flous[i], flousx, flousy, ecran);
if (espace==0)
{
sawer(perso[indice], ppx, ppy, ecran);
} 
if (espace==1)
{
sawer(perso[indicespace], ppx, ppy, ecran);
}
sawer(explo[l], explx, exply, ecran);
sawer(bombe[indicebombe], bombx, bomby, ecran);

if (fondx<-500)
{
sawer(tayara[indicetayara], avionx, aviony, ecran);
avionx-=8;

}
if (fondx<-600 )
{
sawer(parachute[indicepara] ,avionx, aviony, ecran);
paray+=15;

}
if (fondx<-500)
{
sawer(gerr[indicepara],gerrierx, gerriery, ecran);
}

SDL_Flip(ecran);
//end affichage

}

void paintstop()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
if(j<6)
{
j++;
}
else
{
j=0;
}
sawer(back1, fondx, fondy, ecran);



sawer(squelette[k], sqx, sqy, ecran);
sawer(squelette[k], sqx1, sqy1, ecran);
sawer(squelette[k], sqx2, sqy2, ecran);
sawer(squelette[k], sqx3, sqy3, ecran);
sawer(perso[0], ppx, ppy, ecran);
sawer(explo[l], explx, exply, ecran);
sawer(bombe[indicebombe], bombx, bomby, ecran);

if (fondx<-500 )
{
sawer(tayara[indicetayara], avionx, aviony, ecran);
avionx-=8;
}

if (fondx<-600 )
{
sawer(parachute[indicepara] ,avionx, aviony, ecran);
paray+=15;

}
if (fondx<-500)
{
sawer(gerr[indicepara],gerrierx, gerriery, ecran);
}
SDL_Flip(ecran);
//end affichage

}

void paintstopflous()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));


sawer(back1, fondx, fondy, ecran);

if(i<5)
{
i++;
}
else
{
i=0;
}
sawer(back1, fondx, fondy, ecran);
sawer(squelette[k], sqx, sqy, ecran);
sawer(squelette[k], sqx1, sqy1, ecran);
sawer(squelette[k], sqx2, sqy2, ecran);
sawer(squelette[k], sqx3, sqy3, ecran);
sawer(flous[i], flousx, flousy, ecran);
sawer(perso[0], ppx, ppy, ecran);
sawer(explo[l], explx, exply, ecran);
sawer(bombe[indicebombe], bombx, bomby, ecran);


if (fondx<-500 )
{
sawer(tayara[indicetayara] ,avionx, aviony, ecran);
avionx-=8;

}
if (fondx<-600 )
{
sawer(parachute[indicepara] ,avionx, aviony, ecran);
paray+=15;

}


if (fondx<-500)
{
sawer(gerr[indicepara],gerrierx, gerriery, ecran);
}
SDL_Flip(ecran);

//end affichage

}



void coli ()
{
printf("\n%d ",r1);
r=0;
g=0;
b=0;

getPixelColor(ppx+perso[2]->w/2,ppy+perso[2]->h, ecran,&r1,&g1,&b1);

//getPixelColor(ppx,ppy+perso[2]->h, back1,&r2,&g2,&b2);

if((r1==r && g1==g && b1==b ) /*|| (r2==r && g2==g && b2==b)*/ )
   {

}

while (r1!=r && g1!=g && b1!=b )
{
getPixelColor(ppx+perso[2]->w/2,ppy+perso[2]->h, ecran,&r1,&g1,&b1);
ppy+=10;
fondx-=10;
flousx-=10;

if(testf==0)
{

paintstopflous();
}
else
{
paintstop();
}
};

}
